#!/bin/sh


TARFILE=$1
TARRESTOREDIR=$2
RESTOREPATH=$3

echo "restorepath is $RESTOREPATH"

echo "tar restore dir is $TARRESTOREDIR"


function error_exit
{
    echo "$1" 1>&2
    exit 1
}

if [ ! -f $TARFILE ]; then
	error_exit "$TARFILE does not exist"
fi

if [ ! -d $TARRESTOREDIR ]; then

	mkdir -p $TARRESTOREDIR
fi

if tar -C $TARRESTOREDIR -xvf $TARFILE; then
	echo "0"

else
	exit_error "1"
fi

if [ -d $RESTOREPATH ]; then
	if rm $RESTOREPATH; ln -s $TARRESTOREDIR $RESTOREPATH; then
		echo "0"
	else
		exit_error "1"
	fi
else
	if ln -s $TARRESTOREDIR $RESTOREPATH; then
        echo "0"
    else
        exit_error "1"
    fi


fi



